<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="rolesIds">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setRole">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRole } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    rolesId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      rolesIds: [], // 已选的角色列表
      rolesList: [] // 所有角色数据
    }
  },
  created() {
    this.getRoles()
    this.getUserDetailById()
  },
  methods: {

    // 获取所有角色列表
    async getRoles() {
      const res = await getRoles()
      this.rolesList = res.data.rows
    },

    // 获取员工基本信息
    async getUserDetailById() {
      const res = await getUserDetailById(this.rolesId)
      this.rolesIds = res.data.roleIds === null ? [] : res.data.roleIds
    },

    // 确定事件
    async setRole() {
      const res = await assignRole({ id: this.rolesId, roleIds: this.rolesIds })
      this.$message.success(res.message)
      this.$emit('close')
    },

    // 取消按钮事件
    cancleDialog() {

    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 20px;
}
</style>
